<?php

/**
 * 小程序服务商管理系统 v1.0.1
 *
 * 商业运营到axzz.cn官网进行授权使用。
 *
 * (c) cighsen02 <xiayu@959602.com>
 *
 * Create at：2025.05.06
 * Update at: 2025.11.22
 */

namespace App\Exceptions;

use Exception;
use Illuminate\Http\JsonResponse;

/**
 * Summary of JsonResponseException
 */
class JsonResponseException extends Exception
{
    private ?array $data;
    
    /**
     * Summary of __construct
     * 
     * @param string $message
     * @param int $status
     * @param array $data|null
     */
    public function __construct(string $message = '', int $status = 500, ?array $data = null)
    {
        $this->data = $data;

        parent::__construct($message, $status);
    }

    /**
     * Summary of render
     *
     * @return JsonResponse
     */
    public function render(): JsonResponse
    {
        $result = [
            'status'    => false,
            'message'   => $this->getMessage()
        ];

        if ($this->data) $result['data'] = $this->data;

        return new JsonResponse($result, $this->getCode());
    }
}
